// const version = '1.0.0'
const version = '1.0.11'
const client = 'web'
export default {
	async post(url,params,callback){
		assignParam(params || {})
		const data = Object.assign(
			{},
			{
				url,
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'version': params.version,
					'imei': params.imei,
					'client': params.client
				},
				data: params,
				success: res=>{
					if(res.data.code===0){
						callback(res.data)
					}else if(res.data.code===1){
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: res.data.message,
							showCancel:false
						})
					} else if(res.data.code === 999){
						console.log('登录状态失效，重新登录')
					} else {
						uni.hideLoading()
						uni.showModal({
							title: '提示',
							content: res.data.message,
							showCancel:false
						})
					}
				}
			}
		)
		await uni.request(data)
	}
}
const createUUID = function () {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  }
  return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
}
function assignParam(params) {
  let cookieImei = uni.getStorageSync('imei')

  if (!cookieImei) {
    var expiresDate = new Date()
    expiresDate.setTime(expiresDate.getTime() + (43200 * 60 * 1000))
    uni.setStorageSync('imei', createUUID())
    cookieImei = uni.getStorageSync('imei')
  }
  const imei = cookieImei
  params.version = version
  params.imei = imei
  params.client = client
}